package com.ext.portlet.registration.action;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.ext.portlet.city.service.CityLocalServiceUtil;
import com.ext.portlet.community.model.CommunityExtra;
import com.ext.portlet.community.model.impl.CommunityExtraImpl;
import com.ext.portlet.community.service.CommunityExtraLocalServiceUtil;
import com.ext.portlet.registration.DuplicateEmployeeIdException;
import com.ext.portlet.registration.DuplicateRollNoException;
import com.ext.portlet.registration.SecretPinException;
import com.ext.portlet.registration.model.Others;
import com.ext.portlet.registration.model.Parent;
import com.ext.portlet.registration.model.School;
import com.ext.portlet.registration.model.Student;
import com.ext.portlet.registration.model.Teacher;
import com.ext.portlet.registration.model.impl.OthersImpl;
import com.ext.portlet.registration.model.impl.ParentImpl;
import com.ext.portlet.registration.model.impl.SchoolImpl;
import com.ext.portlet.registration.model.impl.StudentImpl;
import com.ext.portlet.registration.model.impl.TeacherImpl;
import com.ext.portlet.registration.service.OthersLocalServiceUtil;
import com.ext.portlet.registration.service.ParentLocalServiceUtil;
import com.ext.portlet.registration.service.SchoolLocalServiceUtil;
import com.ext.portlet.registration.service.StudentLocalServiceUtil;
import com.ext.portlet.registration.service.TeacherLocalServiceUtil;
import com.ext.portlet.registration.util.RegistrationActionUtil;
import com.liferay.counter.service.persistence.CounterUtil;
import com.liferay.portal.ContactBirthdayException;
import com.liferay.portal.DuplicateGroupException;
import com.liferay.portal.DuplicateUserEmailAddressException;
import com.liferay.portal.DuplicateUserScreenNameException;
import com.liferay.portal.GroupFriendlyURLException;
import com.liferay.portal.GroupNameException;
import com.liferay.portal.NoSuchGroupException;
import com.liferay.portal.NoSuchUserException;
import com.liferay.portal.PortalException;
import com.liferay.portal.RequiredGroupException;
import com.liferay.portal.ReservedUserScreenNameException;
import com.liferay.portal.SystemException;
import com.liferay.portal.UserScreenNameException;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.CharPool;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstancePool;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.User;
import com.liferay.portal.model.UserGroup;
import com.liferay.portal.model.impl.LayoutImpl;
import com.liferay.portal.security.auth.ScreenNameValidator;
import com.liferay.portal.service.AddressLocalServiceUtil;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.GroupServiceUtil;
import com.liferay.portal.service.PhoneServiceUtil;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.service.UserGroupLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.base.PrincipalBean;
import com.liferay.portal.service.persistence.UserGroupUtil;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.FriendlyURLNormalizer;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PrefsPropsUtil;
import com.liferay.portal.util.PropsKeys;
import com.liferay.portal.util.PropsValues;


public class AddRegistration extends PortletAction{
	
	public void processAction(
			ActionMapping mapping, ActionForm form, PortletConfig config,
			ActionRequest req, ActionResponse res)
				throws Exception {
		
		HttpServletRequest request = PortalUtil.getHttpServletRequest(req);
        ThemeDisplay theme = (ThemeDisplay)request.getAttribute(WebKeys.THEME_DISPLAY);
        long cId = theme.getCompanyId();
                	
		String cmd = ParamUtil.getString(req, Constants.CMD);
		if (cmd.equals(Constants.MOVE)) {
			String registration = req.getParameter("registration");							
			
			if(registration != null && registration.equals("1")){
				setForward(req, "portlet.ext.registration.schoolForm");				
			}else if(registration != null && registration.equals("2")){
				setForward(req, "portlet.ext.registration.student");
			}else if(registration != null && registration.equals("3")){
				setForward(req, "portlet.ext.registration.teacher");
			}else if(registration != null && registration.equals("4")){
				setForward(req, "portlet.ext.registration.parent");
			}else if(registration != null && registration.equals("5")){
				setForward(req, "portlet.ext.registration.guestForm");
			}else{
				setForward(req, "portlet.ext.registration.view");
			}
		}			
		if (cmd.equals("addSchool")) {
			
			addschool(req, cId);
		}
		if (cmd.equals("addParent")) {
			
			addParent(req, cId);
			
		}	
		if(cmd.equals("addStudent")){
			
			addStudent(req, cId);
			
		}
		if (cmd.equals("addTeacher")) {
			
			addTeacher(req, cId);
			
		}
		if (cmd.equals("addOthers")) {
			
			addOthers(req, cId);
		}	
		if (cmd.equals("validParent")) {		
			
			validParent(req);	
			
		}
		if (cmd.equals("validStudent")) {
			
			validStudent(req);				
		}
		
		if (cmd.equals("validTeacher")) {
			
			validTeacher(req);
						
		}
		if(cmd.equals("createSchool")) {
			
			createSchool(req);
			
		}
		if(cmd.equals("createStudent")) {
			
			createStudent(req);
		}
		
		if(cmd.equals("createParent")) {
			
			createParent(req);
		}
		if(cmd.equals("approveSchool")){	
			
			approveSchool(req, cId);
		}
		if(cmd.equals("approveStudent")) { 
			
			approveStudent(req, cId);
			
		}
		if(cmd.equals("approveOthers")) { 
			
			approveOthers(req, cId);
			
		}
		if(cmd.equals("approveParent")) { 
			
			approveParent(req, cId);
			
		}
		if(cmd.equals("approveTeacher")) { 
			
			approveTeacher(req, cId);
			
		}
		
		if(cmd.equals("viewSchool")){	
			
			viewSchool(req);	
		}
		if(cmd.equals("viewStudent")){				
							
			viewStudent(req);
		}
		if(cmd.equals("viewTeacher")){				
			
			viewTeacher(req);
			
		}
		if(cmd.equals("viewParent")){				
			
			viewParent(req);
		}
		if(cmd.equals("viewOther")){				
			
			viewOther(req);
		}
		if(cmd.equals("removeSchool")) {
			
			removeSchool(req);
		}
		if(cmd.equals("removeStudent")) {
			
			removeStudent(req);
			
		}
		if(cmd.equals("removeParent")) {
			
			removeParent(req);
			
		}
		if(cmd.equals("removeTeacher")) {
			
			removeTeacher(req);
			
		}
		if(cmd.equals("removeOther")) {
			
			removeOther(req);
			
		}
		if(cmd.equals("pendingSchool")) {
			
			pendingSchool(req);
		}
		if(cmd.equals("rejectSchool")) {
			
			rejectSchool(req);
		}
		if(cmd.equals("pendingStudent")) {
			
			pendingStudent(req);
			
		}
		if(cmd.equals("rejectStudent")) {
			
			rejectStudent(req);
			
		}
		if(cmd.equals("pendingParent")) {
			
			pendingParent(req);
			
		}
		if(cmd.equals("rejectParent")) {
			
			rejectParent(req);
			
		}
		if(cmd.equals("pendingTeacher")) {
			
			pendingTeacher(req);
			
		}
		if(cmd.equals("rejectTeacher")) {
			
			rejectTeacher(req);
			
		}
		if(cmd.equals("pendingOthers")) {
			
			pendingOthers(req);
			
		}
		if(cmd.equals("rejectOthers")) {
			
			rejectOthers(req);
			
		}
	}
	
	/**
	 * @param req
	 * @throws PortalException
	 * @throws SystemException
	 */
	private void viewOther(ActionRequest req) throws PortalException,
			SystemException {
		Long othersIds = Long.parseLong(req.getParameter("othersId"));				
		Others others = OthersLocalServiceUtil.getOthers(othersIds);
		req.setAttribute("Others", others);
		setForward(req, "portlet.ext.registration.viewOthersDetails");
	}
	/**
	 * @param req
	 * @throws PortalException
	 * @throws SystemException
	 */
	private void viewParent(ActionRequest req) throws PortalException,
			SystemException {
		Long parentId = Long.parseLong(req.getParameter("parentId"));				
		Parent parent = ParentLocalServiceUtil.getParent(parentId);
		req.setAttribute("Parent", parent);
		setForward(req, "portlet.ext.registration.viewParentDetails");
	}
	/**
	 * @param req
	 * @throws PortalException
	 * @throws SystemException
	 */
	private void viewTeacher(ActionRequest req) throws PortalException,
			SystemException {
		Long teacherId = Long.parseLong(req.getParameter("teacherId"));				
		Teacher teacher = TeacherLocalServiceUtil.getTeacher(teacherId);
		req.setAttribute("Teacher", teacher);
		setForward(req, "portlet.ext.registration.viewTeacherDetails");
	}
	/**
	 * @param req
	 * @throws PortalException
	 * @throws SystemException
	 */
	private void viewStudent(ActionRequest req) throws PortalException,
			SystemException {
		Long studentId = Long.parseLong(req.getParameter("studentId"));				
		Student student = StudentLocalServiceUtil.getStudent(studentId);
		req.setAttribute("student", student);
		setForward(req, "portlet.ext.registration.viewStudentDetails");
	}
	/**
	 * @param req
	 * @throws PortalException
	 * @throws SystemException
	 */
	private void viewSchool(ActionRequest req) throws PortalException,
			SystemException {
		String button = req.getParameter("approve");
		Long schoolId = Long.parseLong(req.getParameter("schoolId"));				
		School school = SchoolLocalServiceUtil.getSchool(schoolId);
		
		if(Validator.isNotNull(button)) {
			req.setAttribute("Button", button);
		}
		
		req.setAttribute("school", school);
		setForward(req, "portlet.ext.registration.viewSchoolDetails");
	}
	/**
	 * @param req
	 * @param cId
	 * @throws PortalException
	 * @throws SystemException
	 * @throws Exception
	 */
	private void removeSchool(ActionRequest req) throws PortalException,
	        SystemException {
		Long schoolId = Long.parseLong(req.getParameter("schoolId"));
		School school = SchoolLocalServiceUtil.getSchool(schoolId);
		school.setStatus(4);
		SchoolLocalServiceUtil.updateSchool(school);
		req.setAttribute("School", school);
		setForward(req, "portlet.ext.registration.success");
	}
	
	private void removeStudent(ActionRequest req) throws PortalException,
			SystemException {
		Long studentId = Long.parseLong(req.getParameter("studentId"));
		Student student = StudentLocalServiceUtil.getStudent(studentId);
		student.setStatus(4);
		StudentLocalServiceUtil.updateStudent(student);
		req.setAttribute("student", student);
		setForward(req, "portlet.ext.registration.success");
	}
	
	private void removeParent(ActionRequest req) throws PortalException,
    		SystemException {
		Long parentId = Long.parseLong(req.getParameter("parentId"));
		Parent parent = ParentLocalServiceUtil.getParent(parentId);
		parent.setStatus(4);
		ParentLocalServiceUtil.updateParent(parent);
		req.setAttribute("Parent", parent);
		setForward(req, "portlet.ext.registration.success");
	}

	private void removeTeacher(ActionRequest req) throws PortalException,
	    	SystemException {
		Long teacherId = Long.parseLong(req.getParameter("teacherId"));
		Teacher teacher = TeacherLocalServiceUtil.getTeacher(teacherId);
		teacher.setStatus(4);
		TeacherLocalServiceUtil.updateTeacher(teacher);
		req.setAttribute("Teacher", teacher);
		setForward(req, "portlet.ext.registration.success");
	}

	private void removeOther(ActionRequest req) throws PortalException,
    		SystemException {
		Long otherId = Long.parseLong(req.getParameter("otherId"));
		Others others = OthersLocalServiceUtil.getOthers(otherId);
		others.setStatus(4);
		OthersLocalServiceUtil.updateOthers(others);
		req.setAttribute("Others", others);
		setForward(req, "portlet.ext.registration.success");
	}

	private void approveTeacher(ActionRequest req, long cId)
			throws PortalException, SystemException, Exception {
		Long teacherId = Long.parseLong(req.getParameter("teacherId"));		
		Teacher teacher = TeacherLocalServiceUtil.getTeacher(teacherId);		
		
		Date birthday = teacher.getBirthDay();
		Calendar cal = Calendar.getInstance();
		cal.setTime(birthday);
		int birthdayMonth = cal.get(Calendar.MONTH);
		int birthdayDay = cal.get(Calendar.DAY_OF_MONTH);
		int birthdayYear = cal.get(Calendar.YEAR);
		
		User newUser = createUser(cId, teacher.getLoginId(), teacher.getFirstName(), 
				teacher.getMiddleName(), teacher.getLastName(), teacher.getGender(),  
				birthdayMonth, birthdayDay, birthdayYear, teacher.getEmailAddress(),teacher.getJobTitle(), "teacher");
		
		long countryId = Long.parseLong(teacher.getCountry());
		long regionId = Long.parseLong(teacher.getState());
		String city = CityLocalServiceUtil.cityName(Long.parseLong(teacher.getCity()));
		
		AddressLocalServiceUtil.addAddress(newUser.getUserId(), Contact.class.getName(), 
				newUser.getContactId(), teacher.getStreet1(), teacher.getStreet2(), teacher.getStreet3(), city, 
				teacher.getPinCode(), regionId, countryId, 11002, false, true);
		
		
		PhoneServiceUtil.addPhone(
				Contact.class.getName(), newUser.getContactId(), teacher.getContactNo(), StringPool.BLANK,
				11008, true);
		
		School school = SchoolLocalServiceUtil.getSchool(teacher.getSchoolId());
		long userIds[] = {newUser.getUserId()};
		long groupId = school.getGroupId();
		
		UserLocalServiceUtil.addGroupUsers(groupId, userIds);	
				
		UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(cId, "Blossoms");
		
		UserGroupUtil.addUser(userGroup.getUserGroupId(), newUser.getPrimaryKey());
		
		
		teacher.setStatus(1);
		teacher.setUserId(newUser.getUserId());
		TeacherLocalServiceUtil.updateTeacher(teacher);
		
		// Loading LAR for User Pages by AYAZ		
		RegistrationActionUtil.addDefaultUserPublicLayouts(newUser);
		
		req.setAttribute("Teacher", teacher);
		setForward(req, "portlet.ext.registration.success");
	}
	/**
	 * @param req
	 * @param cId
	 * @throws PortalException
	 * @throws SystemException
	 * @throws Exception
	 */
	private void approveParent(ActionRequest req, long cId)
			throws PortalException, SystemException, Exception {
		Long parentId = Long.parseLong(req.getParameter("parentId"));		
		Parent parent = ParentLocalServiceUtil.getParent(parentId);
		
		Date birthday = parent.getBirthDay();
		Calendar cal = Calendar.getInstance();
		cal.setTime(birthday);
		int birthdayMonth = cal.get(Calendar.MONTH);
		int birthdayDay = cal.get(Calendar.DAY_OF_MONTH);
		int birthdayYear = cal.get(Calendar.YEAR);
		
		User newUser = createUser(cId, parent.getLoginId(), parent.getFirstName(),
				parent.getMiddleName(), parent.getLastName(), parent.getGender(),  
				birthdayMonth, birthdayDay, birthdayYear, parent.getEmailAddress(),parent.getJobTitle(), "parent");
		
		long countryId = Long.parseLong(parent.getCountry());
		long regionId = Long.parseLong(parent.getState());
		String city = CityLocalServiceUtil.cityName(Long.parseLong(parent.getCity()));
		
		AddressLocalServiceUtil.addAddress(newUser.getUserId(), Contact.class.getName(), 
				newUser.getContactId(), parent.getStreet1(), parent.getStreet2(), parent.getStreet3(),
				city, parent.getPinCode(), regionId, countryId, 11002, false, true);
		
		PhoneServiceUtil.addPhone(
				Contact.class.getName(), newUser.getContactId(), parent.getContactNo(), StringPool.BLANK,
				11008, true);
		
		School school = SchoolLocalServiceUtil.getSchool(parent.getSchoolId());
		long userIds[]={newUser.getUserId()};
		UserLocalServiceUtil.addGroupUsers(school.getGroupId(), userIds);		
		

		UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(cId, "Blossoms");
		
		UserGroupUtil.addUser(userGroup.getUserGroupId(), newUser.getPrimaryKey());
		
		
		parent.setStatus(1);
		parent.setUserId(newUser.getUserId());
		ParentLocalServiceUtil.updateParent(parent);
		
		// Loading LAR for User Pages by AYAZ		
		RegistrationActionUtil.addDefaultUserPublicLayouts(newUser);
		
		req.setAttribute("Parent", parent);
		setForward(req, "portlet.ext.registration.success");
	}
	/**
	 * @param req
	 * @param cId
	 * @throws PortalException
	 * @throws SystemException
	 * @throws Exception
	 */
	private void approveOthers(ActionRequest req, long cId)
			throws PortalException, SystemException, Exception {
		Long othersId = Long.parseLong(req.getParameter("othersId"));		
		Others others = OthersLocalServiceUtil.getOthers(othersId);
		
		Date birthday = others.getBirthDay();
		Calendar cal = Calendar.getInstance();
		cal.setTime(birthday);
		int birthdayMonth = cal.get(Calendar.MONTH);
		int birthdayDay = cal.get(Calendar.DAY_OF_MONTH);
		int birthdayYear = cal.get(Calendar.YEAR);
		
		User newUser = createUser(cId, others.getLoginId(), others.getFirstName(),
				others.getMiddleName(), others.getLastName(), others.getGender(),  
				birthdayMonth, birthdayDay, birthdayYear, others.getEmailAddress(),others.getJobTitle(), "others");
		
		long countryId = Long.parseLong(others.getCountry());
		long regionId = Long.parseLong(others.getState());
		String city = CityLocalServiceUtil.cityName(Long.parseLong(others.getCity()));
		
		AddressLocalServiceUtil.addAddress(newUser.getUserId(), Contact.class.getName(), 
				newUser.getContactId(), others.getStreet1(), others.getStreet2(), others.getStreet3(),
				city, others.getPinCode(), regionId, countryId, 11002, false, true);
	
		PhoneServiceUtil.addPhone(
				Contact.class.getName(), newUser.getContactId(), others.getContactNo(), StringPool.BLANK,
				11008, true);
		
		others.setStatus(1);
		others.setUserId(newUser.getUserId());
		OthersLocalServiceUtil.updateOthers(others);
		
		// Loading LAR for User Pages by AYAZ		
		RegistrationActionUtil.addDefaultUserPublicLayouts(newUser);
		
		req.setAttribute("Others", others);
		setForward(req, "portlet.ext.registration.success");
	}
	/**
	 * @param req
	 * @param cId
	 * @throws PortalException
	 * @throws SystemException
	 * @throws Exception
	 */
	private void approveStudent(ActionRequest req, long cId)
			throws PortalException, SystemException, Exception {
		Long studentId = Long.parseLong(req.getParameter("studentId"));		
		Student student = StudentLocalServiceUtil.getStudent(studentId);
					
		String emailAddress = student.getLoginId()+StringPool.AT+"bench-mark.org";
		
		Date birthday = student.getBirthDay();
		Calendar cal = Calendar.getInstance();
		cal.setTime(birthday);
		int birthdayMonth = cal.get(Calendar.MONTH);
		int birthdayDay = cal.get(Calendar.DAY_OF_MONTH);
		int birthdayYear = cal.get(Calendar.YEAR);
		
		
		User newUser = createUser(cId, student.getLoginId(), student.getFirstName(), 
				student.getMiddleName(), student.getLastName(), student.getGender(), 
				birthdayMonth, birthdayDay, birthdayYear, emailAddress,student.getStandard(), "student");
		
		long countryId = Long.parseLong(student.getCountry());
		long regionId = Long.parseLong(student.getState());
		String city = CityLocalServiceUtil.cityName(Long.parseLong(student.getCity()));
		
		AddressLocalServiceUtil.addAddress(newUser.getUserId(), Contact.class.getName(), 
				newUser.getContactId(), student.getStreet1(), student.getStreet2(), student.getStreet3(),
				city, student.getPinCode(), regionId, countryId, 11002, false, true);
	
		PhoneServiceUtil.addPhone(
				Contact.class.getName(), newUser.getContactId(), student.getContactNo(), StringPool.BLANK,
				11008, true);
		
		School school = SchoolLocalServiceUtil.getSchool(student.getSchoolId());
		long userIds[] = {newUser.getUserId()};
		long groupId = school.getGroupId();
		
		UserLocalServiceUtil.addGroupUsers(groupId, userIds);	
		
		UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(cId, "Blossoms");
		
		UserLocalServiceUtil.addUserGroupUsers(userGroup.getUserGroupId(), userIds);
		
		student.setStatus(1);
		student.setUserId(newUser.getUserId());
		StudentLocalServiceUtil.updateStudent(student);
		
		// Loading LAR for User Pages by AYAZ		
		RegistrationActionUtil.addDefaultUserPublicLayouts(newUser);
		
		req.setAttribute("student", student);
		setForward(req, "portlet.ext.registration.success");
	}
	/**
	 * @param req
	 * @param cId
	 * @throws PortalException
	 * @throws SystemException
	 * @throws Exception
	 */
	private void approveSchool(ActionRequest req, long cId)
			throws PortalException, SystemException, Exception {
		Long schoolId = Long.parseLong(req.getParameter("schoolId"));				
		School school = SchoolLocalServiceUtil.getSchool(schoolId);
		String secretPin = req.getParameter("secretPin");		
		
		Date birthday = school.getBirthDay();
		Calendar cal = Calendar.getInstance();
		cal.setTime(birthday);
		int birthdayMonth = cal.get(Calendar.MONTH);
		int birthdayDay = cal.get(Calendar.DAY_OF_MONTH);
		int birthdayYear = cal.get(Calendar.YEAR);
		
		if(secretPin != null) {
			
			School schoolList=null;
			try {
				schoolList=	SchoolLocalServiceUtil.secretPinValidation(secretPin);
				
				if(schoolList!= null) {
					req.setAttribute("school", school);
					
					throw new SecretPinException();
				}else{
					//approve logic put here
					
					User newUser = createUser(cId, school.getLoginId(), school.getCpFirstName(),
							school.getCpMiddleName(), school.getCpLastName(),  school.getCpGender(),
							birthdayMonth, birthdayDay, birthdayYear, school.getCpEmailAddress(), school.getJobTitle(), "school");
					
					long countryId = Long.parseLong(school.getCountry());
					long regionId = Long.parseLong(school.getState());
					String city = CityLocalServiceUtil.cityName(Long.parseLong(school.getCity()));
					
					
					AddressLocalServiceUtil.addAddress(newUser.getUserId(), Contact.class.getName(), 
							newUser.getContactId(), school.getStreet1(), school.getStreet2(), school.getStreet3(),
							city, school.getPinCode(), regionId, countryId, 11002, false, true);
				
					PhoneServiceUtil.addPhone(
							Contact.class.getName(), newUser.getContactId(), school.getContactNo(), StringPool.BLANK,
							11008, true);
					
					school.setSecretPin(secretPin);
					school.setUserId(newUser.getUserId());
					school.setStatus(1);
					
					String communityName = school.getName().trim();
					String friendlyURL = StringPool.SLASH + communityName.toLowerCase();
					
					// Creating Community 
					// not check Duplicate communityName && friendlyURL
					Group group = GroupServiceUtil.addGroup(communityName,"description", 
							3, friendlyURL, true);
					school.setGroupId(group.getGroupId());
					school = SchoolLocalServiceUtil.updateSchool(school);
					
					CommunityExtra communityExtra = new CommunityExtraImpl();
					communityExtra.setNew(true);
					communityExtra.setCommunityId(CounterUtil.increment(CommunityExtra.class.getName()));
					communityExtra.setCompanyId(cId);
					communityExtra.setGroupId(group.getGroupId());
					communityExtra.setName(group.getName());
					communityExtra.setCreateDate(new Date());
					communityExtra.setType(1);
					communityExtra.setCategory(2);
					CommunityExtraLocalServiceUtil.addCommunityExtra(communityExtra);
					
					
					long userIds[]={newUser.getUserId()};
					UserLocalServiceUtil.addGroupUsers(group.getGroupId(), userIds);
					
					// Loading LAR for Community
					File larFile = RegistrationActionUtil.getBlossomsDefaultLar();
					RegistrationActionUtil.addDefaultLayoutsByLAR(newUser.getUserId(), group.getGroupId(), false, larFile);
															
					// Loading LAR for User Pages by AYAZ
					RegistrationActionUtil.addDefaultUserPublicLayouts(newUser);
					
					UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(cId, "Blossoms");
					
					UserLocalServiceUtil.addUserGroupUsers(userGroup.getUserGroupId(), userIds);
					
					req.setAttribute("school", school);	
					setForward(req, "portlet.ext.registration.success");
				}
				
			}catch(Exception e){
				log.info(e);
				
				if(e instanceof SecretPinException){
					SessionErrors.add(req, e.getClass().getName(), e);
				}else if (e instanceof DuplicateGroupException ||
						 e instanceof GroupFriendlyURLException ||
						 e instanceof GroupNameException ||
						 e instanceof RequiredGroupException) {

					SessionErrors.add(req, e.getClass().getName(), e);
					e.printStackTrace();
				}else {
					throw e;
				}
				setForward(req, "portlet.ext.registration.viewSchoolDetails");
			}
		}
	}
	/**
	 * @param req
	 * @throws PortalException
	 * @throws SystemException
	 */
	private void createParent(ActionRequest req) throws PortalException,
			SystemException {
		String studentId = req.getParameter("studentId");
		Parent parent = ParentLocalServiceUtil.getParent(Long.valueOf(studentId));
		System.out.println("The "+parent.getFirstName()+" Is Being Added");
		parent.setStatus(1);
		ParentLocalServiceUtil.getService().updateParent(parent);
		setForward(req, "portlet.ext.registration.view");
	}
	/**
	 * @param req
	 * @throws PortalException
	 * @throws SystemException
	 */
	private void createStudent(ActionRequest req) throws PortalException,
			SystemException {
		String studentId = req.getParameter("studentId");
		Student student = StudentLocalServiceUtil.getStudent(Long.valueOf(studentId));
		System.out.println("The "+student.getFirstName()+" Is Being Added");
		student.setStatus(1);
		StudentLocalServiceUtil.getService().updateStudent(student);
		setForward(req, "portlet.ext.registration.view");
	}
	/**
	 * @param req
	 * @throws PortalException
	 * @throws SystemException
	 */
	private void createSchool(ActionRequest req) throws PortalException,
			SystemException {
		String schoolId = req.getParameter("schoolId");
		School school = SchoolLocalServiceUtil.getSchool(Long.valueOf(schoolId));
		System.out.println("The "+school.getName()+" Is Being Added");
		school.setStatus(1);
		SchoolLocalServiceUtil.getService().updateSchool(school);
		setForward(req, "portlet.ext.registration.view");
	}
	/**
	 * @param req
	 * @param cId
	 * @throws SystemException
	 * @throws Exception
	 */
	private void addOthers(ActionRequest req, long cId) throws SystemException,
			Exception {
		String firstName = req.getParameter("firstName");
		String middleName = req.getParameter("middleName");
		String lastName = req.getParameter("lastName");
		Boolean gender = ParamUtil.getBoolean(req, "gender");
		String jobTitle = req.getParameter("jobTitle");
		String street1 = req.getParameter("street1");
		String street2 = req.getParameter("street2");
		String street3 = req.getParameter("street3");
		String country = req.getParameter("countryId");
		String state = req.getParameter("regionId");
		String city = req.getParameter("cityId");
		String pinCode = req.getParameter("pinCode");
		String contactNo = req.getParameter("contactNo");
		String emailAddress = req.getParameter("emailId");
		String howUKnow = req.getParameter("howUKnow");
		String loginName = req.getParameter("loginName");
		int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
		int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
		int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
		Date birthDay = PortalUtil.getDate(
				birthdayMonth, birthdayDay, birthdayYear,
				new ContactBirthdayException());
		
		Others otherEmailValidation = OthersLocalServiceUtil.EmailValidation(emailAddress);
		
		
		try{				
			validateScreenName(cId, loginName);
			
			if(Validator.isNotNull(otherEmailValidation)){
				throw new DuplicateUserEmailAddressException();
			}else{					
			Others others = new OthersImpl();
			others.setPrimaryKey(CounterUtil.increment(Others.class.getName()));
			others.setNew(true);
			others.setFirstName(firstName);
			others.setMiddleName(middleName);
			others.setLastName(lastName);
			others.setGender(gender);
			others.setBirthDay(birthDay);
			others.setJobTitle(jobTitle);
			others.setStreet1(street1);
			others.setStreet2(street2);
			others.setStreet3(street3);
			others.setCountry(country);
			others.setState(state);
			others.setCity(city);
			others.setPinCode(pinCode);
			others.setContactNo(contactNo);
			others.setEmailAddress(emailAddress);
			others.setHowUKnow(howUKnow);
			others.setLoginId(loginName);
			
			others.setCreateDate(new Date());
			others.setStatus(0);
			
			OthersLocalServiceUtil.getService().addOthers(others);				
			
			SessionMessages.add(req, "Registered Successfully");				
			
			setForward(req, "portlet.ext.registration.success");
			}
		}catch(Exception e){
			if(e instanceof DuplicateUserEmailAddressException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else if(e instanceof DuplicateUserScreenNameException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else if(e instanceof UserScreenNameException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else if(e instanceof ReservedUserScreenNameException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else{
				throw e;
			}						
			setForward(req, "portlet.ext.registration.guestForm");		
		}
	}
	/**
	 * @param req
	 * @throws SystemException
	 * @throws Exception
	 */
	private void addTeacher(ActionRequest req, long cId) throws SystemException,
			Exception {
		String employeeId = req.getParameter("employeeId");
		String schoolId = req.getParameter("schoolId");
		String firstName = req.getParameter("firstName");
		String middleName = req.getParameter("middleName");
		String lastName = req.getParameter("lastName");
		Boolean gender = ParamUtil.getBoolean(req, "gender");
		String jobTitle = req.getParameter("jobTitle");
		String street1 = req.getParameter("street1");
		String street2 = req.getParameter("street2");
		String street3 = req.getParameter("street3");
		String country = req.getParameter("countryId");
		String state = req.getParameter("regionId");
		String city = req.getParameter("cityId");
		String pinCode = req.getParameter("pinCode");
		String contactNo = req.getParameter("contactNo");
		String emailAddress = req.getParameter("emailId");
		String qualification = req.getParameter("qualification");
		String loginName = req.getParameter("loginName");
		int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
		int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
		int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
		Date birthDay = PortalUtil.getDate(
				birthdayMonth, birthdayDay, birthdayYear,
				new ContactBirthdayException());
		
		Teacher teacheremailValidation = TeacherLocalServiceUtil.getByEmail(emailAddress);
		try{
			validateScreenName(cId, loginName);
			
			if(Validator.isNotNull(teacheremailValidation)){
				throw new DuplicateUserEmailAddressException();
			}else{
				Teacher teacher = new TeacherImpl();
				teacher.setPrimaryKey(CounterUtil.increment(Teacher.class.getName()));
				teacher.setEmployeeId(employeeId);
				teacher.setSchoolId(Long.parseLong(schoolId));
				teacher.setFirstName(firstName);
				teacher.setMiddleName(middleName);
				teacher.setLastName(lastName);
				teacher.setGender(gender);
				teacher.setBirthDay(birthDay);
				teacher.setJobTitle(jobTitle);
				teacher.setStreet1(street1);
				teacher.setStreet2(street2);
				teacher.setStreet3(street3);
				teacher.setCountry(country);
				teacher.setState(state);
				teacher.setCity(city);
				teacher.setPinCode(pinCode);
				teacher.setContactNo(contactNo);
				teacher.setEmailAddress(emailAddress);
				teacher.setQualification(qualification);
				teacher.setStatus(0);
				teacher.setNew(true);
				teacher.setCreateDate(new Date());
				teacher.setLoginId(loginName);
				
				TeacherLocalServiceUtil.getService().addTeacher(teacher);
				SessionMessages.add(req, "Registered Successfully");				
				
				setForward(req, "portlet.ext.registration.success");
			}
		}catch(Exception e){
			if(e instanceof DuplicateUserEmailAddressException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else if(e instanceof ReservedUserScreenNameException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else if(e instanceof UserScreenNameException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else{
				throw e;
			}
			req.setAttribute("employeeId", employeeId);
			req.setAttribute("schoolId", schoolId);						
			setForward(req, "portlet.ext.registration.teacherForm");
		}
	}
	/**
	 * @param req
	 * @throws Exception
	 */
	private void validTeacher(ActionRequest req) throws Exception {
		String employeeId = req.getParameter("employeeId").trim();
		String schoolPin = req.getParameter("schoolPin").trim();
		
		try{
			School school = SchoolLocalServiceUtil.secretPinValidation(schoolPin);
			if(school != null){
				
				Teacher teacher = TeacherLocalServiceUtil
									.teacherValidation(school.getSchoolId(), employeeId);
				if(Validator.isNotNull(teacher)){
					throw new DuplicateEmployeeIdException();
				}
				req.setAttribute("employeeId", employeeId);
				req.setAttribute("schoolId", String.valueOf(school.getSchoolId()));
				setForward(req, "portlet.ext.registration.teacherForm");
				
			}else{
				throw new SecretPinException();
			}
		}catch(Exception e){
			
			if(e instanceof SecretPinException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else if(e instanceof DuplicateEmployeeIdException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else{
				throw e;
			}
			req.setAttribute("employeeId", employeeId);
			req.setAttribute("schoolPin", schoolPin);						
			setForward(req, "portlet.ext.registration.teacher");					
		}
	}	
	
	/**
	 * @param req
	 * @param cId
	 * @throws Exception 
	 */
	private void addStudent(ActionRequest req, long cId)
			throws Exception {
		String schoolId = req.getParameter("schoolId").trim();
		int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
		int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
		int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
		Date birthDay = PortalUtil.getDate(
				birthdayMonth, birthdayDay, birthdayYear,
				new ContactBirthdayException());
		String rollNo = req.getParameter("rollNo");
		String firstName = req.getParameter("firstName");
		String middleName = req.getParameter("middleName");
		String lastName = req.getParameter("lastName");
		Boolean gender = ParamUtil.getBoolean(req, "gender");
		String standard = req.getParameter("standard");
		String street1 = req.getParameter("street1");
		String street2 = req.getParameter("street2");
		String street3 = req.getParameter("street3");
		String country = req.getParameter("countryId");
		String state = req.getParameter("regionId");
		String city = req.getParameter("cityId");
		String pinCode = req.getParameter("pinCode");
		String contactNo = req.getParameter("contactNo");
		String loginName = req.getParameter("loginName");	
		
		
		try{
			validateScreenName(cId, loginName);

		 	Student student = new StudentImpl();
			student.setPrimaryKey(CounterUtil.increment(Student.class.getName()));
			student.setSchoolId(Long.parseLong(schoolId));
			student.setRollNo(rollNo);
			student.setFirstName(firstName);
			student.setMiddleName(middleName);
			student.setLastName(lastName);
			student.setLoginId(loginName);
			student.setGender(gender);
			student.setBirthDay(birthDay);
			student.setStandard(standard);
			student.setStreet1(street1);
			student.setStreet2(street2);
			student.setStreet3(street3);
			student.setCountry(country);
			student.setState(state);
			student.setCity(city);
			student.setPinCode(pinCode);
			student.setContactNo(contactNo);
			student.setStatus(0);
			student.setCreateDate(new Date());
			student.setNew(true);
			
			StudentLocalServiceUtil.getService().addStudent(student);
			SessionMessages.add(req, "Registered Successfully");				
			
			setForward(req, "portlet.ext.registration.success");							
	
			
		}catch(Exception ex){
			
			if(ex instanceof DuplicateUserScreenNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else if(ex instanceof ReservedUserScreenNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else if(ex instanceof UserScreenNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else {
				throw ex;
			}
			req.setAttribute("studentId", rollNo);
			req.setAttribute("schoolId", schoolId);
			setForward(req, "portlet.ext.registration.studentForm");
		}			
	}
	/**
	 * @param req
	 * @throws Exception
	 */
	private void validStudent(ActionRequest req) throws Exception {
		String studentId = req.getParameter("studentId").trim();
		String schoolPin = req.getParameter("schoolPin").trim();
		
		try{
			School school = SchoolLocalServiceUtil.secretPinValidation(schoolPin);
			if(school != null){
				Student student = StudentLocalServiceUtil
								.StudentValidation(school.getSchoolId(), studentId);
				if(student != null){
					throw new DuplicateRollNoException();
				}
				req.setAttribute("studentId", studentId);
				req.setAttribute("schoolId", String.valueOf(school.getSchoolId()));
				setForward(req, "portlet.ext.registration.studentForm");
				
			}else{
				throw new SecretPinException();
			}
		}catch(Exception e){
			
			if(e instanceof SecretPinException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else if(e instanceof DuplicateRollNoException) {
				SessionErrors.add(req, e.getClass().getName(), e);
			}else{
				throw e;
			}
			setForward(req, "portlet.ext.registration.student");					
		}
	}
	/**
	 * @param req
	 * @param cId
	 * @throws Exception 
	 */
	private void addParent(ActionRequest req, long cId) throws Exception {
		String rollNo = req.getParameter("rollNo").trim();
		String schoolId = req.getParameter("schoolId");
		String firstName = req.getParameter("firstName");
		String middleName = req.getParameter("middleName");
		String lastName = req.getParameter("lastName");
		Boolean gender = ParamUtil.getBoolean(req, "gender");
		Integer relation = Integer.parseInt(req.getParameter("relation"));
		String jobTitle = req.getParameter("jobTitle");
		String street1 = req.getParameter("street1");
		String street2 = req.getParameter("street2");
		String street3 = req.getParameter("street3");
		String country = req.getParameter("countryId");
		String state = req.getParameter("regionId");
		String city = req.getParameter("cityId");
		String cityText = req.getParameter("cityText");
		String pinCode = req.getParameter("pinCode");
		String contactNo = req.getParameter("contactNo");
		String emailAddress = req.getParameter("emailId").trim();
		String loginName = req.getParameter("loginName");
		int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
		int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
		int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
		Date birthDay = PortalUtil.getDate(
				birthdayMonth, birthdayDay, birthdayYear,
				new ContactBirthdayException());
		
		try{
			validateScreenName(cId, loginName);

			Parent parent1 = ParentLocalServiceUtil.getByEmailAddress(emailAddress);
			if(Validator.isNotNull(parent1)){						
				throw new DuplicateUserEmailAddressException();
			}else{
				Parent parent = new ParentImpl();
				parent.setPrimaryKey(CounterUtil.increment(Parent.class.getName()));
				//parent.setStudentId(Long.parseLong(studentId));
				parent.setSchoolId(Long.parseLong(schoolId));
				parent.setRollNo(rollNo);
				parent.setFirstName(firstName);
				parent.setMiddleName(middleName);
				parent.setLastName(lastName);
				parent.setLoginId(loginName);
				parent.setGender(gender);
				parent.setBirthDay(birthDay);
				parent.setRelation(relation);
				parent.setJobTitle(jobTitle);
				parent.setStreet1(street1);
				parent.setStreet2(street2);
				parent.setStreet3(street3);
				parent.setCountry(country);
				parent.setState(state);
				parent.setCity(city == null?cityText:city);
				parent.setPinCode(pinCode);
				parent.setContactNo(contactNo);
				parent.setEmailAddress(emailAddress);
				parent.setStatus(0);
				parent.setCreateDate(new Date());
				parent.setNew(true);							
				
				ParentLocalServiceUtil.getService().addParent(parent);
				SessionMessages.add(req, "Registered Successfully");				
				
				setForward(req, "portlet.ext.registration.success");							
			}
			
			
			
		}catch(Exception ex){
			if(ex instanceof DuplicateUserScreenNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else if(ex instanceof ReservedUserScreenNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else if(ex instanceof UserScreenNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else {
				throw ex;
			}
			req.setAttribute("studentId", rollNo);
			req.setAttribute("schoolId", schoolId);
			setForward(req, "portlet.ext.registration.parentForm");
		}
	}
	/**
	 * @param req
	 * @throws Exception
	 */
	private void validParent(ActionRequest req) throws Exception {
		String rollNo = req.getParameter("studentId").trim();
		String schoolPin = req.getParameter("schoolPin").trim();

		try{
			School school = SchoolLocalServiceUtil.secretPinValidation(schoolPin);
			if(school != null){	
				Parent parent = ParentLocalServiceUtil.ParentValidation(rollNo, school.getSchoolId()); 
				
				if(parent != null){
					throw new DuplicateRollNoException();
				}						
				req.setAttribute("studentId", rollNo);
				req.setAttribute("schoolId", String.valueOf(school.getSchoolId()));
				setForward(req, "portlet.ext.registration.parentForm");						
			}else{
				throw new SecretPinException();
			}
		}catch(Exception e){
			
			if(e instanceof SecretPinException){
				SessionErrors.add(req, e.getClass().getName(), e);
			}else if(e instanceof DuplicateRollNoException) {
				SessionErrors.add(req, e.getClass().getName(), e);
			}else{
				throw e;
			}
			setForward(req, "portlet.ext.registration.parent");					
		}
	}
	/**
	 * @param req
	 * @param cId
	 * @throws Exception
	 */
	private void addschool(ActionRequest req, long cId) throws Exception {
		String name = req.getParameter("name");
		String street1 = req.getParameter("street1");
		String street2 = req.getParameter("street2");
		String street3 = req.getParameter("street3");
		String board = req.getParameter("board");
		String others = req.getParameter("others");
		String countryId = req.getParameter("countryId");
		String regionId = req.getParameter("regionId");
		String cityId = req.getParameter("cityId");
		String cityIdText = req.getParameter("cityIdText");
		String pinCode = req.getParameter("pinCode");
		String contactNo = req.getParameter("contactNo");
		String webSite = req.getParameter("webSite");
		String emailId = req.getParameter("emailId").trim();
		Integer noOfStudents = ParamUtil.getInteger(req, "noOfStudents");
		Integer noOfTeachers = ParamUtil.getInteger(req, "noOfTeachers");
		String firstName = req.getParameter("firstName");
		String middleName = req.getParameter("middleName");
		String lastName = req.getParameter("lastName");
		Boolean gender = ParamUtil.getBoolean(req, "gender");
		String jobTitle = ParamUtil.getString(req, "jobTitle");
		String cpContactNo = req.getParameter("cpContactNo");
		String loginName = req.getParameter("loginName");
	
		int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
		int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
		int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
		Date dateOfEstablishment = PortalUtil.getDate(
				birthdayMonth, birthdayDay, birthdayYear,
				new ContactBirthdayException());
		
		int birthDayMonth = ParamUtil.getInteger(req, "birthDayMonth");
		int birthDayDay = ParamUtil.getInteger(req, "birthDayDay");
		int birthDayYear = ParamUtil.getInteger(req, "birthDayYear");
		Date birthDay = PortalUtil.getDate(
				birthDayMonth, birthDayDay, birthDayYear,
				new ContactBirthdayException());
	
		try{
			validateScreenName(cId, loginName);
			validateName(name);
			//validateFriendlyURL(name);
			School school1 = SchoolLocalServiceUtil.getByEmail(emailId); 
			
			if(Validator.isNotNull(school1)){						
			throw new DuplicateUserEmailAddressException();
		    } else{
				School school = new SchoolImpl();
				school.setPrimaryKey(CounterUtil.increment(School.class.getName()));
				school.setName(name);
				school.setDateOfEstablishment(dateOfEstablishment);
				school.setStreet1(street1);
				school.setStreet2(street2);
				school.setStreet3(street3);
				school.setBoard(board);
				school.setOthers(others);
				school.setCountry(countryId);
				school.setState(regionId);
				school.setCity(cityId == null? cityIdText : cityId);
				school.setPinCode(pinCode);
				school.setContactNo(contactNo);
				school.setWebsite(webSite);
				school.setCpEmailAddress(emailId);
				school.setNoOfStudents(noOfStudents);
				school.setNoOfTeachers(noOfTeachers);
				school.setCpFirstName(firstName);
				school.setCpMiddleName(middleName);
				school.setCpLastName(lastName);
				school.setCpGender(gender);
				school.setBirthDay(birthDay);
				school.setJobTitle(jobTitle);
				school.setCpContactNo(cpContactNo);
				school.setLoginId(loginName);
				
				//school.setDateOfEstablishment(dateOfEstablishment);
				school.setCreateDate(new Date());
				school.setStatus(0);
				school.setIpAddress(req.getRemoteUser());
				school.setNew(true);
				
				SchoolLocalServiceUtil.getService().addSchool(school);
				SessionMessages.add(req, "Registered Successfully");				
				
				setForward(req, "portlet.ext.registration.success");						
		    	}	
			
			
		}catch(Exception ex){
			if(ex instanceof DuplicateUserScreenNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else if(ex instanceof ReservedUserScreenNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else if(ex instanceof UserScreenNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else if(ex instanceof DuplicateUserEmailAddressException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else if(ex instanceof GroupNameException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else if(ex instanceof GroupFriendlyURLException){
				SessionErrors.add(req, ex.getClass().getName(), ex);
				
			}else {
				throw ex;
			}
			setForward(req, "portlet.ext.registration.schoolForm");
			
		}	
						
	}
	
//pending and reject
	
	private void pendingSchool(ActionRequest req) throws PortalException, SystemException {
		Long schoolId = Long.parseLong(req.getParameter("schoolId"));				
		School school = SchoolLocalServiceUtil.getSchool(schoolId);
		school.setStatus(2);
		SchoolLocalServiceUtil.updateSchool(school);
		setForward(req, "portlet.ext.registration.view");
		
	}
	
	private void rejectSchool(ActionRequest req) throws PortalException, SystemException {
		Long schoolId = Long.parseLong(req.getParameter("schoolId"));				
		School school = SchoolLocalServiceUtil.getSchool(schoolId);
		school.setStatus(3);
		SchoolLocalServiceUtil.updateSchool(school);
		setForward(req, "portlet.ext.registration.view");
		
	}
	
	private void pendingStudent(ActionRequest req) throws PortalException, SystemException {
		Long studentId = Long.parseLong(req.getParameter("studentId"));				
		Student student = StudentLocalServiceUtil.getStudent(studentId);
		student.setStatus(2);
		StudentLocalServiceUtil.updateStudent(student);
		setForward(req, "portlet.ext.registration.view");
		
	}
	
	private void rejectStudent(ActionRequest req) throws PortalException, SystemException {
		Long studentId = Long.parseLong(req.getParameter("studentId"));				
		Student student = StudentLocalServiceUtil.getStudent(studentId);
		student.setStatus(3);
		StudentLocalServiceUtil.updateStudent(student);
		setForward(req, "portlet.ext.registration.view");
		
	}
	
	private void pendingParent(ActionRequest req) throws PortalException, SystemException {
		Long parentId = Long.parseLong(req.getParameter("parentId"));				
		Parent parent = ParentLocalServiceUtil.getParent(parentId);
		parent.setStatus(2);
		ParentLocalServiceUtil.updateParent(parent);
		setForward(req, "portlet.ext.registration.view");
		
	}
	
	private void rejectParent(ActionRequest req) throws PortalException, SystemException {
		Long parentId = Long.parseLong(req.getParameter("parentId"));				
		Parent parent = ParentLocalServiceUtil.getParent(parentId);
		parent.setStatus(3);
		ParentLocalServiceUtil.updateParent(parent);
		setForward(req, "portlet.ext.registration.view");
		
	}
	
	private void pendingTeacher(ActionRequest req) throws PortalException, SystemException {
		Long teacherId = Long.parseLong(req.getParameter("teacherId"));				
		Teacher teacher = TeacherLocalServiceUtil.getTeacher(teacherId);
		teacher.setStatus(2);
		TeacherLocalServiceUtil.updateTeacher(teacher);
		setForward(req, "portlet.ext.registration.view");
		
	}
	
	private void rejectTeacher(ActionRequest req) throws PortalException, SystemException {
		Long teacherId = Long.parseLong(req.getParameter("teacherId"));				
		Teacher teacher = TeacherLocalServiceUtil.getTeacher(teacherId);
		teacher.setStatus(3);
		TeacherLocalServiceUtil.updateTeacher(teacher);
		setForward(req, "portlet.ext.registration.view");
		
	}
	
	private void pendingOthers(ActionRequest req) throws PortalException, SystemException {
		Long othersId = Long.parseLong(req.getParameter("othersId"));				
		Others others = OthersLocalServiceUtil.getOthers(othersId);
		others.setStatus(2);
		OthersLocalServiceUtil.updateOthers(others);
		setForward(req, "portlet.ext.registration.view");
		
	}
	
	private void rejectOthers(ActionRequest req) throws PortalException, SystemException {
		Long othersId = Long.parseLong(req.getParameter("othersId"));				
		Others others = OthersLocalServiceUtil.getOthers(othersId);
		others.setStatus(3);
		OthersLocalServiceUtil.updateOthers(others);
		setForward(req, "portlet.ext.registration.view");
		
	}
			// Method to approve the account
			protected User createUser(
		            long companyId, String screenName, String firstName,
		            String middleName, String lastName, boolean male, 
		            int birthdayMonth, int birthdayDay, int birthdayYear, String emailAdd,String jobTitle, String comments)
		        throws Exception {
				long[] roleIds = {};
		        long creatorUserId = 0;
		        boolean autoPassword = false;
		        String password1 = screenName;
		        String password2 = password1;
		        boolean autoScreenName = false;
		        String emailAddress = emailAdd;
		        String openId = StringPool.BLANK;
		        Locale locale = Locale.US;
		        //String jobTitle = StringPool.BLANK;
		        int prefixId = 0;
		        int suffixId = 0;
		        
		        long[] groupIds = null;
		
		        long[] emptyLong = {};
		        
		        long[] userGroupIds = null;
		        boolean sendEmail = false;
		        ServiceContext serviceContext = null;
		
		        User user = UserLocalServiceUtil.addUser(
		            creatorUserId, companyId, autoPassword, password1, password2,
		            autoScreenName, screenName, emailAddress, openId, locale, firstName,
		            middleName, lastName, prefixId, suffixId, male, birthdayMonth,
		            birthdayDay, birthdayYear, jobTitle, groupIds, emptyLong,
		            roleIds, userGroupIds, sendEmail, serviceContext);
		        user.setComments(comments);
		        UserLocalServiceUtil.updateUser(user);
			 return user;
			
			}
												
			protected String getFriendlyURL(String friendlyURL) {
				friendlyURL = GetterUtil.getString(friendlyURL);

				return FriendlyURLNormalizer.normalize(friendlyURL);
			}
			
			
			
			protected void validateScreenName(
					long companyId, String screenName)
				throws PortalException, SystemException {

				if (Validator.isNull(screenName)) {
					throw new UserScreenNameException();
				}

				ScreenNameValidator screenNameValidator =
					(ScreenNameValidator)InstancePool.get(
						PropsValues.USERS_SCREEN_NAME_VALIDATOR);

				if (screenNameValidator != null) {
					if (!screenNameValidator.validate(companyId, screenName)) {
						throw new UserScreenNameException();
					}
				}

				if (Validator.isNumber(screenName) ) {

					throw new UserScreenNameException();
				}

				for (char c : screenName.toCharArray()) {
					if ((!Validator.isChar(c)) && (!Validator.isDigit(c)) &&
						(c != CharPool.DASH) && (c != CharPool.PERIOD) &&
						(c != CharPool.UNDERLINE)) {

						throw new UserScreenNameException();
					}
				}

				String[] anonymousNames = PrincipalBean.ANONYMOUS_NAMES;

				for (int i = 0; i < anonymousNames.length; i++) {
					if (screenName.equalsIgnoreCase(anonymousNames[i])) {
						throw new UserScreenNameException();
					}
				}
				User user = null;
				try{
				 user = UserLocalServiceUtil.getService().getUserByScreenName(companyId, screenName);
				}catch(NoSuchUserException ex){
					//
				}
				
				//User user = userPersistence.fetchByC_SN(companyId, screenName);

				if (user != null) {
					throw new DuplicateUserScreenNameException();
				}

				String friendlyURL = StringPool.SLASH + screenName;
				Group group = null;
				try{
				 group = GroupLocalServiceUtil.getService().getFriendlyURLGroup(companyId, friendlyURL);
				}catch(NoSuchGroupException ge){
					//
				}
				//Group group = groupPersistence.fetchByC_F(companyId, friendlyURL);

				if (group != null) {
					throw new DuplicateUserScreenNameException();
				}

				int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);

				if (exceptionType != -1) {
					throw new UserScreenNameException(
						new GroupFriendlyURLException(exceptionType));
				}

				String[] reservedScreenNames = PrefsPropsUtil.getStringArray(
					companyId, PropsKeys.ADMIN_RESERVED_SCREEN_NAMES,
					StringPool.NEW_LINE, PropsValues.ADMIN_RESERVED_SCREEN_NAMES);

				for (int i = 0; i < reservedScreenNames.length; i++) {
					if (screenName.equalsIgnoreCase(reservedScreenNames[i])) {
						throw new ReservedUserScreenNameException();
					}
				}
			}
			
			
			protected void validateFriendlyURL(String friendlyURL)
				throws PortalException, SystemException {

				if (Validator.isNull(friendlyURL)) {
					return;
				}

				int exceptionType = LayoutImpl.validateFriendlyURL(friendlyURL);

				if (exceptionType != -1) {
					throw new GroupFriendlyURLException(exceptionType);
				}				
			}

			protected void validateName(String name)
				throws PortalException, SystemException {

				if ((Validator.isNull(name)) || (Validator.isNumber(name)) ||
					(name.indexOf(StringPool.COMMA) != -1) ||
					(name.indexOf(StringPool.STAR) != -1)) {
					throw new GroupNameException();
				}		
			}
			
			private static Log log = LogFactory.getLog(AddRegistration.class);
}
